﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGJHBrowse : Form
    {
        //查询所有提交状态的计划单
        string strSelectAllSubmitWGJH = "select b.* from wgjhb b,wgjha a where b.计划单号=a.计划单号";
        //获取计划关联的第一个合同号
        string strGetContractNo = "select top 1 jhb024 from scjhb_table,wgjhht where jhb001=生产计划号 and jhb002=生产计划项次 and 计划单号='@计划单号' and 计划项次=@计划项次";
        //获取计划关联的最早的交货期
        string strGetJHQ = "select min(jhb009) from scjhb_table,wgjhht where jhb001=生产计划号 and jhb002=生产计划项次 and 计划单号='@计划单号' and 计划项次=@计划项次";
        //更新打印标记
        string strUpdateExportMark = "update wgjhb set 已导出=1 where 序号='@序号'";

        public frmWGJHBrowse()
        {
            InitializeComponent();
        }

        private void rbDateRange_CheckedChanged(object sender, EventArgs e)
        {
            dtpFrom.Enabled = rbDateRange.Checked;
            dtpTo.Enabled = rbDateRange.Checked;
        }

        private void rbModel_CheckedChanged(object sender, EventArgs e)
        {
            txtModel.Enabled = rbModel.Checked;
        }

        private void rbContractNo_CheckedChanged(object sender, EventArgs e)
        {
            txtContract.Enabled = rbContractNo.Checked;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = strSelectAllSubmitWGJH;
            if (cmbDocStatus.Text.Trim().Length > 0)
            {
                strTmpSelectString += " and a.单据状态=" + Convert.ToString(cmbDocStatus.SelectedIndex - 1);
            }
            if (rbDateRange.Checked)
            {
                strTmpSelectString += " and a.提交时间 between '" + dtpFrom.Value.ToString("yyyy-MM-dd") + "' and '" + dtpTo.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (rbModel.Checked)
            {
                if (txtModel.Text.Trim().Length == 0)
                {
                    clsTools.ShowWarningMsg("请填写要查询的型号规格！");
                    txtModel.Focus();
                    return;
                }
                strTmpSelectString += " and b.型号规格 like '" + txtModel.Text.Trim() + "'";
            }
            if (rbContractNo.Checked)
            {
                if (txtContract.Text.Trim().Length == 0)
                {
                    clsTools.ShowWarningMsg("请填写要查询的合同号！");
                    txtContract.Focus();
                    return;
                }
                strTmpSelectString += " and (b.计划单号 in (select 计划单号 from wgjhht,scjhb_table where 生产计划号=jhb001 and jhb024 like '" + txtContract.Text.Trim() + "') or 合同号 like '"+txtContract .Text .Trim ()+"')";
            }
            switch (cmbItemStatus.SelectedIndex)
            {
                case 0:
                    strTmpSelectString += " and b.已导出=0";
                    break;
                case 1:
                    strTmpSelectString += " and b.已导出=1";
                    break;
            }
            strTmpSelectString += " order by WG型号规格";
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            DataTable tmpdt = sqt.GetDataTable(strTmpSelectString);
            dgv1.DataSource = tmpdt;
            dgv1.AutoResizeColumns();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtModel.Clear();
            txtContract.Clear();
            dgv1.DataSource = null;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count == 0)
            {
                clsTools.ShowWarningMsg("表中没有可以导出的数据！");
                return;
            }
            if (dgv1.SelectedRows.Count == 0)
            {
                clsTools.ShowWarningMsg("请先选择要导出的数据！");
                return;
            }
            int[] idx = new int[dgv1.SelectedRows.Count];
            for (int i = 0; i < dgv1.SelectedRows.Count; i++)
            {
                idx[i] = dgv1.SelectedRows[i].Index;
            }
            System.Array.Sort(idx);
            SqlTools.SqlTools sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.xls|*.xls";
            string strCurrentPlanID = DateTime.Now.ToString("yy") + "-" + clsTools.GetString(clsTools.GetInt(WG计划管理工具.Properties.Settings.Default.CurrentJHNo)).Trim();
            if (txtCurrentJHNo.Text.Trim().Length > 0)
            {
                sfd.FileName = strCurrentPlanID+".xls";
            }
            sfd.ShowDialog();
            string strTmpFileName = sfd.FileName;
            if (strTmpFileName.Trim().Length > 0)
            {
                DataTable tmpdt = new DataTable();
                tmpdt.Columns.Add("计划单号");
                tmpdt.Columns.Add("序号");
                tmpdt.Columns.Add("等级");
                tmpdt.Columns.Add("合同号");
                tmpdt.Columns.Add("型号规格");
                tmpdt.Columns.Add("端电极");
                tmpdt.Columns.Add("尺寸");
                tmpdt.Columns.Add("温度特性");
                tmpdt.Columns.Add("电压");
                tmpdt.Columns.Add("容量");
                tmpdt.Columns.Add("精度");
                tmpdt.Columns.Add("合同数量");
                tmpdt.Columns.Add("申购数量");
                tmpdt.Columns.Add("合同交期");
                tmpdt.Columns.Add("计划日期");
                //tmpdt.Columns.Add("对应配套型号");
                //tmpdt.Columns.Add("实际订量");k
                //tmpdt.Columns.Add("到货日期");l
                //tmpdt.Columns.Add("到货数量");
                //tmpdt.Columns.Add("欠交数量");n
                //tmpdt.Columns.Add("计划处理情况");
                //tmpdt.Columns.Add("供应商回复情况");p
                tmpdt.Columns.Add("备注");
                DataRow r;
                try
                {
                    for (int i = 0; i < idx .Length ; i++)
                    {
                        r = tmpdt.NewRow();
                        if (i == 0)
                        {
                            r["计划单号"] = "'" + strCurrentPlanID;
                        }
                        r["序号"] = Convert.ToString(i + 1);
                        r["等级"] = dgv1.Rows[idx[i]].Cells["型号规格"].Value.ToString().Substring(0, dgv1.Rows[idx[i]].Cells["型号规格"].Value.ToString().IndexOf('-'));
                        if (dgv1.Rows[idx[i]].Cells["合同号"].Value.ToString().Trim() != "多项")
                        {
                            r["合同号"] = dgv1.Rows[idx[i]].Cells["合同号"].Value.ToString().Trim();
                        }
                        else
                        {
                            r["合同号"] = clsTools.GetString(sqt.DoCmdScalarData(strGetContractNo.Replace("@计划单号", dgv1.Rows[idx[i]].Cells["计划单号"].Value.ToString().Trim())
                                                                                                                                                .Replace("@计划项次", dgv1.Rows[idx[i]].Cells["计划项次"].Value.ToString().Trim())));
                        }
                        r["合同交期"] = clsTools.GetString(sqt.DoCmdScalarData(strGetJHQ.Replace("@计划单号", dgv1.Rows[idx[i]].Cells["计划单号"].Value.ToString().Trim())
                                                                                                                                            .Replace("@计划项次", dgv1.Rows[idx[i]].Cells["计划项次"].Value.ToString().Trim())));
                        if (r["合同交期"].ToString().Trim().Length == 8)
                        {
                            r["合同交期"] = r["合同交期"].ToString().Trim().Insert(6, "-").Insert(4, "-");
                        }
                        if (r["等级"].ToString().Trim() == "M")
                        {
                            r["等级"] = "工业级";
                        }
                        if (r["等级"].ToString().Trim() == "GJB")
                        {
                            r["等级"] = "国军标";
                        }
                        r["端电极"] = "三端";
                        r["计划日期"] = DateTime.Now.ToString("yyyy-MM-dd");
                        r["型号规格"] = dgv1.Rows[idx[i]].Cells["WG型号规格"].Value.ToString().Trim();
                        string[] strModel = dgv1.Rows[idx[i]].Cells["WG型号规格"].Value.ToString().Trim().Split('-');
                        if (strModel.Length  == 5)
                        {
                            //r["型号"] = strModel[1];
                            r["尺寸"] = string.Empty;
                            r["温度特性"] = strModel[2];
                            r["电压"] = strModel[3];
                            r["容量"] = strModel[4].Substring(0, 3);
                            r["精度"] = strModel[4].Substring(3, 1);
                        }
                        if (strModel.Length  == 6)
                        {
                            //r["型号"] = strModel[1];
                            r["尺寸"] ="'"+ strModel[2];
                            r["温度特性"] = strModel[3];
                            r["电压"] = strModel[4];
                            r["容量"] = strModel[5].Substring(0, 3);
                            r["精度"] = strModel[5].Substring(3, 1);
                        }
                        if (strModel.Length  == 7)
                        {
                            if (r["型号规格"].ToString().StartsWith("SAST-G")|| r["型号规格"].ToString().StartsWith("SAST-GJB")|| r["型号规格"].ToString().StartsWith("SAST-QJ"))
                            {
                                //r["型号"] = strModel[2];
                                r["尺寸"] = "'" + strModel[3];
                                r["温度特性"] = strModel[4];
                                r["电压"] = strModel[5];
                                r["容量"] = strModel[6].Substring(0, 3);
                                r["精度"] = strModel[6].Substring(3, 1);
                            }
                            else
                            {
                                //r["型号"] = strModel[1] ;
                                r["尺寸"] = "'" + strModel[2];
                                r["温度特性"] = strModel[3];
                                r["电压"] = strModel[4];
                                r["容量"] = strModel[5].Substring(0, 3);
                                r["精度"] = strModel[5].Substring(3, 1);
                            }
                        }
                        if (strModel.Length == 8)
                        {
                            //r["型号"] = strModel[2];
                            r["尺寸"] = "'" + strModel[3];
                            r["温度特性"] = strModel[4];
                            r["电压"] = strModel[5];
                            r["容量"] = strModel[6].Substring(0, 3);
                            r["精度"] = strModel[6].Substring(3, 1);
                        }
                        r["合同数量"] = dgv1.Rows[idx[i]].Cells["生产计划数量"].Value.ToString();
                        r["申购数量"] = dgv1.Rows[idx[i]].Cells["WG计划数量"].Value.ToString();
                        r["备注"] = dgv1.Rows[idx[i]].Cells["备注"].Value.ToString();
                        tmpdt.Rows.Add(r);
                    }
                    ExcelHelper.DataToExcel dte = new ExcelHelper.DataToExcel();
                    if (dte.ExportToExcel(tmpdt, strTmpFileName, true, "物料需求计划"))
                    {
                        clsTools.ShowInfo("EXCEL文件导出成功！");
                        for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                        {
                            sqt.DoCmdExecuteNonQuery(strUpdateExportMark.Replace("@序号", dgv1.Rows[idx[i]].Cells["序号"].Value.ToString()));
                        }
                        //设置当前计划号
                        WG计划管理工具.Properties.Settings.Default.CurrentJHNo = clsTools.GetString(clsTools.GetInt(WG计划管理工具.Properties.Settings.Default.CurrentJHNo) + 1).Trim();
                        WG计划管理工具.Properties.Settings.Default.Save();
                    }
                    else
                    {
                        clsTools.ShowErrMsg(dte.strErrMsg);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < dgv1.Rows.Count; i++)
                {
                    dgv1.Rows[i].Selected = true;
                }
            }
        }

        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            lblSelectCount.Text = "已选定" + dgv1.SelectedRows.Count.ToString() + "行";
        }

        private void frmWGJHBrowse_Load(object sender, EventArgs e)
        {
            cmbItemStatus.SelectedIndex = 0;
            cmbDocStatus.SelectedIndex = 2;
        }
    }
}